﻿//********************************************************************
// WaveFileReader.h: interface for the CWavFile class.
// 波形文件类
// 功能：波形文件采样数据获取
// 编制人：李文
// 时间：2008.11.26
//*********************************************************************

#pragma once

#ifndef WAVE_FILE_READER_DLLCLASS
#define WAVE_FILE_READER_DLLCLASS __declspec(dllimport)
#endif
#include <string>
#include <Mmsystem.h>
#pragma comment(lib,"Winmm.lib")
using std::string;
using std::wstring;
#include <TChar.h>
#include "..\ErrorCodeDefine.h"
#include "..\UnCopyable\UnCopyable.h"
#include "..\WavEditForm\TypeDefine.h"
#pragma comment(lib,"..\\UnCopyable.lib")
#pragma warning(disable: 4251)
class WAVE_FILE_READER_DLLCLASS CWavFile:private UnCopyable  
{
public:
    static int m_percent;       //当前数据处理的进度
    static bool SaveDataToFile(const _TCHAR* filename,const INT16* pdata,DWORD databytes,DWORD samplerate);
    void SetStart(DWORD offset);
    DWORD GetBufSize(DWORD size) const;
    bool ReadHuge();
    WORD GetBitsPerSample() const;
    WORD GetChannels() const;
    DWORD GetSampleRate() const;
    BOOL GetWavSampleValue(INT16 *pBuf,DWORD channel,DWORD dataSize);
    CWavFile();
    virtual ~CWavFile();
    BOOL Open(const _TCHAR* strFileName);
    void Close();
    BOOL Read(INT16* pBuf);
    DWORD   GetDataSize() const;
    int GetLastErrorCode() const;
    const char* GetSourceData(DWORD mapsize);
private:
    bool InitWavHeader();
    bool CheckWavData();
    INT16* GetDestMappingData(DWORD mapsize);
    void FreeHuge();
    //每次内存映射的字节大小（32MB）
    enum {MAPPINGSIZE = 32*1024*1024};
    DWORD m_wavheadsize;         //wav文件头的字节数
    DWORD m_mapoffset;           //记录当前wav文件的映射偏移起点,不含文件头
    DWORD m_samplerate;          //单通道采样率
    WORD m_bitsPerSample;
    WORD m_channels;
    bool m_IsHuge;               //指示是否使用了大文件的读取方式
    tstring m_filename;
    HMMIO         m_hmmio;       //打开的文件的句柄
    MMCKINFO      m_ck;         //child chunk info
    MMCKINFO      m_ckRiff;    //parent chunk info
    DWORD         m_dwSize;    //文件数据的字节长度
    const char*         m_pbData;    //指向文件数据的指针，不分左右通道的所有字节数据
    int        m_errorInfo;        //所有操作失败的错误信息代码
};




